package tongwentongshu.com.app.network;

import android.content.Intent;
import android.util.Log;

import de.greenrobot.event.EventBus;
import rx.Subscriber;
import tongwentongshu.com.app.App;
import tongwentongshu.com.app.activity.LoginActivity;
import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.bean.InfoEvent;
import tongwentongshu.com.app.bean.LoginEvent;
import tongwentongshu.com.app.bean.MoneyEvent;
import tongwentongshu.com.app.utils.ToastUtil;

public abstract class ResponseSubscriber<T> extends Subscriber<T> {
    private final String onSuccess = "1";
    private final String landing = "-99";
    private final String money = "1000";
    private final String onInfo = "3";

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        onFailure(e);
    }

    @Override
    public void onNext(T t) {
        AppItem app = (AppItem) t;
        Log.e("AppItem", t.getClass().getName() + "-" + app.toString());
        if (app.ret.equals(onSuccess)) {
            onSuccess(t, app.msg);
        } else if (app.ret.equals(landing)) {
//            App.getApplication().startActivity(new Intent(App.getApplication(), LoginActivity.class));
            EventBus.getDefault().post(new LoginEvent());
        } else if (app.ret.equals(onInfo)) {
//            App.getApplication().startActivity(new Intent(App.getApplication(), LoginActivity.class));
            EventBus.getDefault().post(new InfoEvent(app.msg));
        } else if (app.ret.equals(money)) {
//            App.getApplication().startActivity(new Intent(App.getApplication(), LoginActivity.class));
            EventBus.getDefault().post(new MoneyEvent());
        } else {
            ToastUtil.show(App.getContext(), app.msg);
        }


    }


    public abstract void onFailure(Throwable e);

    public abstract void onSuccess(T t, String msg);

}
